home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / psd.zip / CMUSCHEM.EL next >
Lisp/Scheme  |  1992-07-10  |  13KB  |  316 lines

  1. ;;; cmuscheme.el -- Scheme process in a buffer. Adapted from tea.el.
  2. ;;; Copyright Olin Shivers (1988)
  3. ;;; Please imagine a long, tedious, legalistic 5-page gnu-style copyright
  4. ;;; notice appearing here to the effect that you may use this code any
  5. ;;; way you like, as long as you don't charge money for it, remove this
  6. ;;; notice, or hold me liable for its results.
  7. ;;;
  8. ;;;    This is a customisation of comint-mode (see comint.el)
  9. ;;;
  10. ;;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  11. ;;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  12. ;;; 8/88
  13. ;;;
  14. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  15. ;;; merge them into the master source.
  16. ;;;
  17. ;;; Fix log:
  18. ;;; Removed -emacs flag from process invocation. It's only useful for
  19. ;;;     cscheme, and makes cscheme assume it's running under xscheme.el,
  20. ;;;     which messes things up royally. A bug. 2/15/89 Olin
  21. ;;;
  22. ;;; NOTE: MIT cscheme, when invoked with the -emacs flag, has a special
  23. ;;; user interface that communicates process state back to the superior emacs
  24. ;;; by outputing special control sequences. The gnumacs package, xscheme.el,
  25. ;;; has lots and lots of special purpose code to read these control sequences,
  26. ;;; and so is very tightly integrated with the cscheme process. Cmuscheme mode
  27. ;;; does *not* provide this functionality. If you are a cscheme user, you
  28. ;;; may prefer to use the xscheme.el/cscheme -emacs interaction. Interested
  29. ;;; parties are invited to port xscheme functionality on top of comint mode...
  30.  
  31. ;; YOUR .EMACS FILE
  32. ;;=============================================================================
  33. ;; Some suggestions for your .emacs file.
  34. ;;
  35. ;; ; If cmuscheme lives in some non-standard directory, you must tell emacs
  36. ;; ; where to get it. This may or may not be necessary.
  37. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  38. ;;
  39. ;; ; Autoload run-scheme from file cmuscheme.el
  40. ;; (autoload 'run-scheme "cmuscheme"
  41. ;;           "Run an inferior Scheme process."
  42. ;;           t)
  43. ;;
  44. ;; ; Files ending in ".scm" are Scheme source, 
  45. ;; ; so put their buffers in scheme-mode.
  46. ;; (setq auto-mode-alist 
  47. ;;       (cons '("\\.scm$" . scheme-mode)  
  48. ;;             auto-mode-alist))
  49. ;;
  50. ;; ; Define C-c C-t to run my favorite command in inferior scheme mode:
  51. ;; (setq cmuscheme-load-hook
  52. ;;       '((lambda () (define-key inferior-scheme-mode-map "\C-c\C-t"
  53. ;;                                'favorite-cmd))))
  54. ;;;
  55. ;;; Unfortunately, scheme.el defines run-scheme to autoload from xscheme.el.
  56. ;;; This will womp your declaration to autoload run-scheme from cmuscheme.el
  57. ;;; if you haven't loaded cmuscheme in before scheme. Two fixes:
  58. ;;; - load cmuscheme.el in your .emacs: (load-library 'cmuscheme)
  59. ;;; - change autoload declaration in scheme.el to point to cmuscheme.el:
  60. ;;;   (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  61. ;;;   *or* just delete the autoload declaration from scheme.el altogether,
  62. ;;;   which will allow the autoload in your .emacs to have its say.
  63.  
  64. (provide 'cmuscheme)
  65. (require 'scheme)
  66. (require 'comint)
  67.  
  68. ;;; INFERIOR SCHEME MODE STUFF
  69. ;;;============================================================================
  70.  
  71. (defvar inferior-scheme-mode-hook nil
  72.   "*Hook for customising inferior-scheme mode.")
  73. (defvar inferior-scheme-mode-map nil)
  74.  
  75. (cond ((not inferior-scheme-mode-map)
  76.        (setq inferior-scheme-mode-map
  77.          (full-copy-sparse-keymap comint-mode-map))
  78.        (define-key scheme-mode-map "\M-\C-x" ;gnu convention
  79.                'scheme-send-definition)
  80.        (define-key inferior-scheme-mode-map "\C-cl"    'scheme-load-file)
  81.        (define-key inferior-scheme-mode-map "\C-ck"    'scheme-compile-file)
  82.        (scheme-mode-commands inferior-scheme-mode-map))) 
  83.  
  84. ;; Install the process communication commands in the scheme-mode keymap.
  85. (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
  86. (define-key scheme-mode-map "\C-ce"    'scheme-send-definition)
  87. (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition-and-go)
  88. (define-key scheme-mode-map "\C-cr"    'scheme-send-region)
  89. (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region-and-go)
  90. (define-key scheme-mode-map "\C-cc"    'scheme-compile-definition)
  91. (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
  92. (define-key scheme-mode-map "\C-cz"    'switch-to-scheme)
  93. (define-key scheme-mode-map "\C-cl"    'scheme-load-file)
  94. (define-key scheme-mode-map "\C-ck"    'scheme-compile-file) ;k for "kompile"
  95.  
  96. (defun inferior-scheme-mode ()
  97.   "Major mode for interacting with an inferior Scheme process.
  98.  
  99. The following commands are available:
  100. \\{inferior-scheme-mode-map}
  101.  
  102. A Scheme process can be fired up with M-x run-scheme.
  103.  
  104. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  105. inferior-scheme-mode-hook (in that order).
  106.  
  107. You can send text to the inferior Scheme process from other buffers containing
  108. Scheme source.  
  109.     switch-to-scheme switches the current buffer to the Scheme process buffer.
  110.     scheme-send-definition sends the current definition to the Scheme process.
  111.     scheme-compile-definition compiles the current definition.
  112.     scheme-send-region sends the current region to the Scheme process.
  113.     scheme-compile-region compiles the current region.
  114.  
  115.     scheme-send-definition-and-go, scheme-compile-definition-and-go,
  116.         scheme-send-region-and-go, and scheme-compile-region-and-go
  117.         switch to the Scheme process buffer after sending their text.
  118.  
  119. Commands:
  120. Return after the end of the process' output sends the text from the 
  121.     end of process to point.
  122. Return before the end of the process' output copies the sexp ending at point
  123.     to the end of the process' output, and sends it.
  124. Delete converts tabs to spaces as it moves back.
  125. Tab indents for Scheme; with argument, shifts rest
  126.     of expression rigidly with the current line.
  127. C-M-q does Tab on each line starting within following expression.
  128. Paragraphs are separated only by blank lines.  Semicolons start comments.
  129. If you accidentally suspend your process, use \\[comint-continue-subjob]
  130. to continue it."
  131.   (interactive)
  132.   (comint-mode)
  133.   ;; Customise in inferior-scheme-mode-hook
  134.   (setq comint-prompt-regexp "^[^>]*>+ *") ; OK for cscheme, oaklisp, T,...
  135.   (scheme-mode-variables)
  136.   (setq major-mode 'inferior-scheme-mode)
  137.   (setq mode-name "Inferior Scheme")
  138.   (setq mode-line-process '(": %s"))
  139.   (use-local-map inferior-scheme-mode-map)
  140.   (setq comint-input-filter (function scheme-input-filter))
  141.   (setq comint-input-sentinel (function ignore))
  142.   (setq comint-get-old-input (function scheme-get-old-input))
  143.   (run-hooks 'inferior-scheme-mode-hook))
  144.  
  145. (defun scheme-input-filter (str)
  146.   "Don't save anything matching inferior-scheme-filter-regexp"
  147.   (not (string-match inferior-scheme-filter-regexp str)))
  148.  
  149. (defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  150.   "*Input matching this regexp are not saved on the history list.
  151. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  152.  
  153. (defun scheme-get-old-input ()
  154.   "Snarf the sexp ending at point"
  155.   (save-excursion
  156.     (let ((end (point)))
  157.       (backward-sexp)
  158.       (buffer-substring (point) end))))
  159.  
  160. (defun scheme-args-to-list (string)
  161.   (if (equal string "")            ;added by tm@cs.tut.fi Feb 92.
  162.       '()
  163.     (let ((where (string-match "[ \t]" string)))
  164.       (cond ((null where) (list string))
  165.         ((not (= where 0))
  166.          (cons (substring string 0 where)
  167.            (scheme-args-to-list (substring string (+ 1 where)
  168.                            (length string)))))
  169.         (t (let ((pos (string-match "[^ \t]" string)))
  170.          (if (null pos)
  171.              nil
  172.            (scheme-args-to-list (substring string pos
  173.                            (length string))))))))))
  174.  
  175. (defvar scheme-program-name "scheme"
  176.   "*Program invoked by the scheme and run-scheme commands")
  177.  
  178. (defun scheme (arg)
  179.   "Like run-scheme, except prompts for a command line."
  180.   (interactive "sExtra arguments to Scheme: ")
  181.   (switch-to-buffer
  182.    (apply 'make-comint (append (list "scheme" scheme-program-name nil)
  183.                    (scheme-args-to-list arg))))
  184.   (inferior-scheme-mode))
  185.  
  186. (defun run-scheme (arg)
  187.   "Run an inferior Scheme process, input and output via buffer *scheme*.
  188. With argument, it asks for a command line.  Take the program name from the
  189. variable scheme-program-name.  Runs the hooks from inferior-scheme-mode-hook
  190. \(after the comint-mode-hook is run).
  191. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  192.   (interactive "P")
  193.   (if arg (call-interactively 'scheme)
  194.       (scheme "")))
  195.  
  196. (defun scheme-send-region (start end)
  197.   "Send the current region to the inferior Scheme process."
  198.   (interactive "r")
  199.   (send-region "scheme" start end)
  200.   (send-string "scheme" "\n"))
  201.  
  202. (defun scheme-send-definition ()
  203.   "Send the current definition to the inferior Scheme process."
  204.   (interactive)
  205.   (save-excursion
  206.    (end-of-defun)
  207.    (let ((end (point)))
  208.      (beginning-of-defun)
  209.      (scheme-send-region (point) end))))
  210.  
  211. (defvar scheme-compile-exp-command "(compile '%s)"
  212.   "*Template for issuing commands to compile arbitrary Scheme expressions.")
  213.  
  214. (defun scheme-compile-region (start end)
  215.   "Compile the current region in the inferior Scheme process
  216. \(A BEGIN is wrapped around the region: (BEGIN <region>))"
  217.   (interactive "r")
  218.   (send-string "scheme" (format scheme-compile-exp-command
  219.                                 (format "(begin %s)"
  220.                         (buffer-substring start end))))
  221.   (send-string "scheme" "\n"))
  222.  
  223. (defun scheme-compile-definition ()
  224.   "Compile the current definition in the inferior Scheme process."
  225.   (interactive)
  226.   (save-excursion
  227.    (end-of-defun)
  228.    (let ((end (point)))
  229.      (beginning-of-defun)
  230.      (scheme-compile-region (point) end))))
  231.  
  232. (defun switch-to-scheme (eob-p)
  233.   "Switch to the *scheme* buffer.
  234. With argument, positions cursor at end of buffer."
  235.   (interactive "P")
  236.   (pop-to-buffer "*scheme*")
  237.   (cond (eob-p
  238.      (push-mark)
  239.      (goto-char (point-max)))))
  240.  
  241. (defun scheme-send-region-and-go (start end)
  242.   "Send the current region to the inferior Scheme process,
  243. and switch to the process buffer."
  244.   (interactive "r")
  245.   (scheme-send-region start end)
  246.   (switch-to-scheme t))
  247.  
  248. (defun scheme-send-definition-and-go ()
  249.   "Send the current definition to the inferior Scheme, 
  250. and switch to the process buffer."
  251.   (interactive)
  252.   (scheme-send-definition)
  253.   (switch-to-scheme t))
  254.  
  255. (defun scheme-compile-definition-and-go ()
  256.   "Compile the current definition in the inferior Scheme, 
  257. and switch to the process buffer."
  258.   (interactive)
  259.   (scheme-compile-definition)
  260.   (switch-to-scheme t))
  261.  
  262. (defun scheme-compile-region-and-go (start end)
  263.   "Compile the current region in the inferior Scheme, 
  264. and switch to the process buffer."
  265.   (interactive "r")
  266.   (scheme-compile-region start end)
  267.   (switch-to-scheme t))
  268.  
  269. (defvar scheme-source-modes '(scheme-mode)
  270.   "*Used to determine if a buffer contains Scheme source code.
  271. If it's loaded into a buffer that is in one of these major modes, it's
  272. considered a scheme source file by scheme-load-file and scheme-compile-file.
  273. Used by these commands to determine defaults.")
  274.  
  275. (defvar scheme-prev-l/c-dir/file nil
  276.   "Caches the (directory . file) pair used in the last scheme-load-file or
  277. scheme-compile-file command. Used for determining the default in the 
  278. next one.")
  279.  
  280. (defun scheme-load-file (file-name)
  281.   "Load a Scheme file into the inferior Scheme process."
  282.   (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
  283.                   scheme-source-modes t)) ; T because LOAD 
  284.                                                           ; needs an exact name
  285.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  286.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  287.                        (file-name-nondirectory file-name)))
  288.   (send-string "scheme" (concat "(load \""
  289.                 file-name
  290.                 "\"\)\n"))
  291.   (switch-to-scheme t))
  292.  
  293. (defun scheme-compile-file (file-name)
  294.   "Compile a Scheme file in the inferior Scheme process."
  295.   (interactive (comint-get-source "Compile Scheme file: "
  296.                   scheme-prev-l/c-dir/file
  297.                   scheme-source-modes
  298.                   nil)) ; NIL because COMPILE doesn't
  299.                                         ; need an exact name.
  300.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  301.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  302.                        (file-name-nondirectory file-name)))
  303.   (send-string "scheme" (concat "(compile-file \""
  304.                 file-name
  305.                 "\"\)\n"))
  306.   (switch-to-scheme t))
  307.  
  308.  
  309. ;;; Do the user's customisation...
  310.  
  311. (defvar cmuscheme-load-hook nil
  312.   "This hook is run when cmuscheme is loaded in.
  313. This is a good place to put keybindings.")
  314.     
  315. (run-hooks 'cmuscheme-load-hook)
  316.